package middle;

import java.util.Arrays;
import util.PrintUtils;

public class Solution_406 {

    public static void main(String[] args) {
//        PrintUtils.transfer("[[7,0],[4,4],[7,1],[5,0],[6,1],[5,2]]");
//        PrintUtils.transfer("[[6,0],[5,0],[4,0],[3,2],[2,2],[1,4]]");
        Solution_406 model = new Solution_406();
        PrintUtils.arrayInt(model.reconstructQueue(new int[][]{{7,0},{4,4},{7,1},{5,0},{6,1},{5,2}}));
        System.out.println();
        PrintUtils.arrayInt(model.reconstructQueue(new int[][]{{6,0},{5,0},{4,0},{3,2},{2,2},{1,4}}));
    }

    public int[][] reconstructQueue(int[][] people) {
        int[][] array = new int[people.length][];
        Arrays.sort(people, (a, b) -> {
            int m = a[0] - b[0];
            return m == 0 ? b[1] - a[1] : m;
        });
        for (int[] person : people) {
            for (int i = 0, count = 0; i < array.length; i++) {
                if (array[i] == null) {
                    if (count == person[1]) {
                        array[i] = person;
                        break;
                    }
                    count++;
                }
            }
        }
        return array;
    }
}
